from django.shortcuts import render_to_response, HttpResponseRedirect
from django.template import RequestContext

from apps.participants.models import CourseParticipant
from apps.management import Management
from models import *
from options import Options
from functions import course_reverse

import logging

logger = logging.getLogger('pet.apps.course.manage')

def manage(request, TheCourseParticipant, TheCourse):
    CanAddCourses = False
    if request.user.has_perm('course.add_course') or (TheCourseParticipant and TheCourseParticipant.status == "INSTRUCTOR" and Options.get_option("Instructor Course Creation")):
        CanAddCourses = True
    CourseDict = {}
    for course_to_manage in Course.objects.all():
        course_participant = CourseParticipant.find(course_to_manage.course_id,user=request.user)
        if request.user.has_perm('course.change_course') or (course_participant and (course_participant.status == "INSTRUCTOR" or course_participant.status == "ASSISTANT")):
            CourseDict[course_to_manage] = True
        else:
            CourseDict[course_to_manage] = False

    return render_to_response('course/manage.html',{'Courses':CourseDict,'CanAddCourses':CanAddCourses},context_instance=RequestContext(request))
Management.register_interface('Course','ASSISTANT;INSTRUCTOR;SUPERUSER','manage',manage)
    
def edit(request, TheCourseParticipant, TheCourse, course_to_edit=None):
    logger.debug("TheCourseParticipant=%s, TheCourse=%s, course_to_edit=%s",TheCourseParticipant, TheCourse, course_to_edit)
     
    if (not course_to_edit) and (not request.user.has_perm('course.add_course')) and TheCourseParticipant and (TheCourseParticipant.status != "INSTRUCTOR" or not Options.get_option("Instructor Course Creation")):
        #User cannot add course
        logger.info("User %s does not have permission to add courses", request.user)
        raise Http404
    if course_to_edit and (not request.user.has_perm('course.edit_course')) and TheCourseParticipant and TheCourseParticipant.status != "INSTRUCTOR" and TheCourseParticipant.status != "ASSISTANT":
        #User cannot edit course
        logger.info("User %s does not have permission to edit course %s",request.user, course)
        raise Http404
    
    if course_to_edit: TheCourse = Course.find_or_404(course_to_edit)
    else: TheCourse = None
    
    if request.method == 'POST':
        Form = CourseEditForm(request.POST, request.FILES, instance=TheCourse)
        if Form.is_valid():
            if Form.has_changed():
                NewCourse = Form.save()
                if NewCourse and TheCourseParticipant and not course_to_edit:
                    NewCourseParticipant = CourseParticipant(user=TheCourseParticipant.user, status=TheCourseParticipant.status, course=NewCourse)
                    NewCourseParticipant.save()
            else:
                logger.form_unchanged("Course Form unchanged")
            if 'Save' in request.POST and request.POST['Save'] == 'SaveReturn':
                return HttpResponseRedirect(course_reverse('management',TheCourse,app='course'))
        else:
            logger.debug("Course Form invalid")
    else:
        Form = CourseEditForm(instance=TheCourse)
        
    return render_to_response('course/edit.html',{'form':Form,'TheCourse':TheCourse},context_instance=RequestContext(request))
Management.register_interface('Course','ASSISTANT;INSTRUCTOR;SUPERUSER','edit',edit)
